#include "raylib.h"
#include <vector>
#include <cstring>
#include <cstdlib>  // 添加这行来使用 abs 函数

const int CELL_SIZE = 20;  // 每个细胞的大小
const int GRID_WIDTH = 40; // 网格宽度
const int GRID_HEIGHT = 30; // 网格高度

class GameOfLife {
private:
    std::vector<bool> grid;
    std::vector<bool> nextGrid;
    bool isPaused;

    int countNeighbors(int x, int y) {
        int count = 0;
        for(int i = -1; i <= 1; i++) {
            for(int j = -1; j <= 1; j++) {
                if(i == 0 && j == 0) continue;
                int newX = (x + i + GRID_WIDTH) % GRID_WIDTH;
                int newY = (y + j + GRID_HEIGHT) % GRID_HEIGHT;
                if(grid[newY * GRID_WIDTH + newX]) count++;
            }
        }
        return count;
    }

public:
    GameOfLife() : grid(GRID_WIDTH * GRID_HEIGHT), 
                  nextGrid(GRID_WIDTH * GRID_HEIGHT),
                  isPaused(true) {}

    void toggle(int x, int y) {
        if(x >= 0 && x < GRID_WIDTH && y >= 0 && y < GRID_HEIGHT) {
            grid[y * GRID_WIDTH + x] = !grid[y * GRID_WIDTH + x];
        }
    }

    void update() {
        if(isPaused) return;
        
        for(int y = 0; y < GRID_HEIGHT; y++) {
            for(int x = 0; x < GRID_WIDTH; x++) {
                int neighbors = countNeighbors(x, y);
                bool currentCell = grid[y * GRID_WIDTH + x];
                
                if(currentCell) {
                    nextGrid[y * GRID_WIDTH + x] = (neighbors == 2 || neighbors == 3);
                } else {
                    nextGrid[y * GRID_WIDTH + x] = (neighbors == 3);
                }
            }
        }
        grid.swap(nextGrid);
    }

    void draw() {
        for(int y = 0; y < GRID_HEIGHT; y++) {
            for(int x = 0; x < GRID_WIDTH; x++) {
                Rectangle cellRect = {
                    (float)(x * CELL_SIZE),
                    (float)(y * CELL_SIZE),
                    (float)CELL_SIZE - 1,
                    (float)CELL_SIZE - 1
                };
                DrawRectangleRec(cellRect, 
                    grid[y * GRID_WIDTH + x] ? BLACK : LIGHTGRAY);
            }
        }
    }

    void reset() {
        std::fill(grid.begin(), grid.end(), false);
        std::fill(nextGrid.begin(), nextGrid.end(), false);
    }

    void setPattern(int patternId) {
        reset(); // 清除当前网格
        isPaused = true; // 加载预设后通常暂停，让用户观察

        // 确保图案不会超出边界，选择一个偏移量
        int offsetX = 2;
        int offsetY = 2;

        switch (patternId) {
            case 1: // Glider (滑翔者)
                if (offsetX + 2 < GRID_WIDTH && offsetY + 2 < GRID_HEIGHT) {
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 2] = true;
                }
                break;
            case 2: // Blinker (闪烁者 - 初始为横条)
                if (offsetX + 2 < GRID_WIDTH && offsetY < GRID_HEIGHT) {
                    grid[offsetY * GRID_WIDTH + offsetX + 0] = true;
                    grid[offsetY * GRID_WIDTH + offsetX + 1] = true;
                    grid[offsetY * GRID_WIDTH + offsetX + 2] = true;
                }
                break;
            case 3: // Toad (蟾蜍)
                if (offsetX + 3 < GRID_WIDTH && offsetY + 1 < GRID_HEIGHT) {
                    // .XXX
                    // XXX.
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 3] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 2] = true;
                }
                break;
            case 4: // Beacon (信标)
                if (offsetX + 3 < GRID_WIDTH && offsetY + 3 < GRID_HEIGHT) {
                    // XX..
                    // XX..
                    // ..XX
                    // ..XX
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 3] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 3] = true;
                }
                break;
            case 5: // Pulsar (脉冲星)
                {
                    // 脉冲星是一个周期为3的大型振荡器
                    int cx = offsetX + 8; // 中心点X
                    int cy = offsetY + 8; // 中心点Y
                    
                    if (cx + 6 < GRID_WIDTH && cy + 6 < GRID_HEIGHT && 
                        cx - 6 >= 0 && cy - 6 >= 0) {
                        // 水平线
                        for (int i = -6; i <= 6; i += 3) {
                            if (i == 0) continue; // 跳过中心
                            
                            // 上下两组水平线
                            for (int j = -4; j <= 4; j++) {
                                if (j == 0 || abs(j) == 3) continue;
                                grid[(cy + i) * GRID_WIDTH + (cx + j)] = true;
                            }
                            
                            // 左右两组水平线
                            for (int j = -4; j <= 4; j++) {
                                if (j == 0 || abs(j) == 3) continue;
                                grid[(cy + j) * GRID_WIDTH + (cx + i)] = true;
                            }
                        }
                    }
                }
                break;
            case 6: // Gosper Glider Gun (高斯帕滑翔者枪)
                {
                    // 这是一个能产生无限滑翔者的图案
                    int x = offsetX + 5;
                    int y = offsetY + 5;
                    
                    if (x + 36 < GRID_WIDTH && y + 9 < GRID_HEIGHT) {
                        // 左侧方块
                        grid[(y + 4) * GRID_WIDTH + (x + 0)] = true;
                        grid[(y + 4) * GRID_WIDTH + (x + 1)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 0)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 1)] = true;
                        
                        // 右侧方块
                        grid[(y + 2) * GRID_WIDTH + (x + 34)] = true;
                        grid[(y + 2) * GRID_WIDTH + (x + 35)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 34)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 35)] = true;
                        
                        // 左侧图案
                        grid[(y + 2) * GRID_WIDTH + (x + 12)] = true;
                        grid[(y + 2) * GRID_WIDTH + (x + 13)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 11)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 15)] = true;
                        grid[(y + 4) * GRID_WIDTH + (x + 10)] = true;
                        grid[(y + 4) * GRID_WIDTH + (x + 16)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 10)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 14)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 16)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 17)] = true;
                        grid[(y + 6) * GRID_WIDTH + (x + 10)] = true;
                        grid[(y + 6) * GRID_WIDTH + (x + 16)] = true;
                        grid[(y + 7) * GRID_WIDTH + (x + 11)] = true;
                        grid[(y + 7) * GRID_WIDTH + (x + 15)] = true;
                        grid[(y + 8) * GRID_WIDTH + (x + 12)] = true;
                        grid[(y + 8) * GRID_WIDTH + (x + 13)] = true;
                        
                        // 中间图案
                        grid[(y + 0) * GRID_WIDTH + (x + 24)] = true;
                        grid[(y + 1) * GRID_WIDTH + (x + 22)] = true;
                        grid[(y + 1) * GRID_WIDTH + (x + 24)] = true;
                        grid[(y + 2) * GRID_WIDTH + (x + 20)] = true;
                        grid[(y + 2) * GRID_WIDTH + (x + 21)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 20)] = true;
                        grid[(y + 3) * GRID_WIDTH + (x + 21)] = true;
                        grid[(y + 4) * GRID_WIDTH + (x + 20)] = true;
                        grid[(y + 4) * GRID_WIDTH + (x + 21)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 22)] = true;
                        grid[(y + 5) * GRID_WIDTH + (x + 24)] = true;
                        grid[(y + 6) * GRID_WIDTH + (x + 24)] = true;
                    }
                }
                break;
            case 7: // Lightweight Spaceship (LWSS, 轻型飞船)
                if (offsetX + 4 < GRID_WIDTH && offsetY + 3 < GRID_HEIGHT) {
                    // .XX..
                    // X..X.
                    // X...X
                    // XXXXX
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 3] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 4] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 3] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 4] = true;
                }
                break;
            case 8: // Pentadecathlon (十五周期振荡器)
                if (offsetX + 2 < GRID_WIDTH && offsetY + 9 < GRID_HEIGHT) {
                    // 一个垂直的长条，中间有特殊结构
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 3) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 4) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 5) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 6) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 7) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 7) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 8) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 9) * GRID_WIDTH + offsetX + 1] = true;
                }
                break;
            case 9: // R-pentomino (R-五聚体，一个小型但能长时间演化的图案)
                if (offsetX + 2 < GRID_WIDTH && offsetY + 2 < GRID_HEIGHT) {
                    // .XX
                    // XX.
                    // .X.
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 0) * GRID_WIDTH + offsetX + 2] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 0] = true;
                    grid[(offsetY + 1) * GRID_WIDTH + offsetX + 1] = true;
                    grid[(offsetY + 2) * GRID_WIDTH + offsetX + 1] = true;
                }
                break;
            default:
                break;
        }
    }

    void togglePause() {
        isPaused = !isPaused;
    }

    bool isPausedState() const {
        return isPaused;
    }
};

int main() {
    InitWindow(GRID_WIDTH * CELL_SIZE, GRID_HEIGHT * CELL_SIZE, "Game of Life");
    int currentFPS = 5;  // 默认FPS设为5
    SetTargetFPS(currentFPS);

    GameOfLife game;

    while (!WindowShouldClose()) {
        // 处理输入
        if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) {
            Vector2 mousePos = GetMousePosition();
            int gridX = mousePos.x / CELL_SIZE;
            int gridY = mousePos.y / CELL_SIZE;
            game.toggle(gridX, gridY);
        }

        // FPS控制
        if (IsKeyPressed(KEY_UP) && currentFPS < 60) {
            currentFPS += 1;
            SetTargetFPS(currentFPS);
        }
        if (IsKeyPressed(KEY_DOWN) && currentFPS > 1) {
            currentFPS -= 1;
            SetTargetFPS(currentFPS);
        }

        if (IsKeyPressed(KEY_SPACE)) {
            game.togglePause();
        }

        if (IsKeyPressed(KEY_R)) {
            game.reset();
        }

        // 新增：处理数字键加载预设图案
        if (IsKeyPressed(KEY_ONE)) {
            game.setPattern(1);
        }
        if (IsKeyPressed(KEY_TWO)) {
            game.setPattern(2);
        }
        if (IsKeyPressed(KEY_THREE)) {
            game.setPattern(3);
        }
        if (IsKeyPressed(KEY_FOUR)) {
            game.setPattern(4);
        }
        if (IsKeyPressed(KEY_FIVE)) {
            game.setPattern(5);
        }
        if (IsKeyPressed(KEY_SIX)) {
            game.setPattern(6);
        }
        if (IsKeyPressed(KEY_SEVEN)) {
            game.setPattern(7);
        }
        if (IsKeyPressed(KEY_EIGHT)) {
            game.setPattern(8);
        }
        if (IsKeyPressed(KEY_NINE)) {
            game.setPattern(9);
        }

        // 更新
        game.update();

        // 绘制
        BeginDrawing();
        ClearBackground(WHITE);
        
        game.draw();

        // 显示状态和帮助信息
        DrawText(game.isPausedState() ? "PAUSED" : "RUNNING", 10, 10, 20, RED);
        DrawText(TextFormat("FPS: %d", currentFPS), 150, 10, 20, RED);
        DrawText("Space: Pause/Resume | R: Reset | Click: Toggle Cell", 
                10, GRID_HEIGHT * CELL_SIZE - 50, 20, DARKGRAY);
        DrawText("Up/Down: Adjust Speed | 1-9: Load Pattern", 
                10, GRID_HEIGHT * CELL_SIZE - 30, 20, DARKGRAY);

        EndDrawing();
    }

    CloseWindow();
    return 0;
}
